package com.dyw.ycgovfinance.mvp.news.newshome;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dyw.ycgovfinance.R;
import com.dyw.ycgovfinance.adapter.ViewPagerFragmentAdapterWithTab;
import com.dyw.ycgovfinance.app.CustomApplication;
import com.dyw.ycgovfinance.bean.NewsKindBean;
import com.dyw.ycgovfinance.mvp.base.BaseFragment;
import com.dyw.ycgovfinance.mvp.news.newschart.FragmentNewsChart;
import com.dyw.ycgovfinance.mvp.news.newslist.FragmentNewsList;
import com.dyw.ycgovfinance.util.LogUtil;
import com.dyw.ycgovfinance.util.ToastUtil;
import com.dyw.ycgovfinance.util.schedulers.SchedulerProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/20.
 */

public class FragmentNewsHome  extends BaseFragment implements NewsHomeContract.View{


    //默认的是isFirstVisibles true ,刚进来的时候分两种情况
    //1.如果切过来的时候 setVisibleHint在OnCreate之前调用，那isFirstVisible为true，这个时候不进行任何操作；
    //等方法执行到onActivityCreated的时候，在执行一次setUserVisibleHint 这个时候把默认的是isFirstVisibles 设为false
    //然后这个时候调用setUserVISIBLEhINT为true  去获取数据
    //2.如果是后面再次切过来，那isFirstVisible已经为false，那就可以直接通过setUserVisibleHint获取数据了

    //代表这个界面是不是第一次被使用
    boolean isFirstUsed = true;
    //是否显示过子fragment
    boolean isShowSonFragment = false;

    NewsHomeContract.Presenter mPresenter;

    public static FragmentNewsHome newInstance() {
        FragmentNewsHome fragment = new FragmentNewsHome();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = new NewsHomePresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());
    }


    private TabLayout tablayout;
    private ViewPager tabvp;

    List<Fragment> mTabFragmentList = new ArrayList<>();
    List<String> mTabTitleList = new ArrayList<>();
    ViewPagerFragmentAdapterWithTab mTabFragmentAdapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news_home, container, false);
        initData();
        assignView(view);
        //initView();

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //fragment 可见的情况下 进行加载数据。不可见则不加载
        if (getUserVisibleHint() == true) {
            if (isFirstUsed == true) {
                isFirstUsed = false;
                setUserVisibleHint(true);
            }
            //不可见，说明fragment已经初始话完成,就直接把isFirstVisible改为false 再次切过来的时候就直接可以加载数据了
        } else {
            isFirstUsed = false;
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        // LogUtil.e("CartVisible");
        if (getUserVisibleHint()) {
            LogUtil.e("MyShuoShuoVisible");
            //如果不是firstvisible才去进行下面的操作  因为第一次很可能出现各种问题
            if (isFirstUsed == false) {
                if (isShowSonFragment == false){
                    getNewsKind();
                    //initView();
                }
            }
        } else {
            LogUtil.e("faXianFragmentVisible");
        }
    }

    @Override
    public void initData() {
     /*   mTabTitleList.add("新闻资讯");
        mTabTitleList.add("图片新闻");
        mTabTitleList.add("通知公告");
        mTabTitleList.add("政策法规");
        mTabTitleList.add("财政报表");*/
    }

    @Override
    public void assignView(View view) {
        tablayout = (TabLayout) view.findViewById(R.id.tablayout);
        tabvp = (ViewPager) view.findViewById(R.id.tabvp);
    }

    @Override
    public void initView() {
        //initTabAndFragment();
    }

    //初始化tab和fragment
    private void initTabAndFragmentWithNewsKind(List<NewsKindBean> newsKindBeanList) {

        isShowSonFragment = true;
        for (NewsKindBean newsKindBean:newsKindBeanList){
            mTabTitleList.add(newsKindBean.getName());
            mTabFragmentList.add(FragmentNewsList.newInstance(newsKindBean));

        }
        mTabTitleList.add("财政数据");
        mTabFragmentList.add(FragmentNewsChart.newInstance(null));
        mTabFragmentAdapter = new ViewPagerFragmentAdapterWithTab(getChildFragmentManager(), mTabFragmentList, mTabTitleList, getActivity());


        for (NewsKindBean newsKindBean:newsKindBeanList){
            tablayout.addTab(tablayout.newTab().setText(newsKindBean.getName()));

        }
        tablayout.addTab(tablayout.newTab().setText("财政数据"));
        //tablayout.setupWithViewPager(tabvp);

        tabvp.setOffscreenPageLimit(newsKindBeanList.size()+1);
        tabvp.setAdapter(mTabFragmentAdapter);
        tabvp.setCurrentItem(0);

        tablayout.setupWithViewPager(tabvp);
        tablayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tabvp.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    private void initTabTitleList(){

    }

    @Override
    public void getNewsKind() {
        mPresenter.getNewsKind();
    }

    @Override
    public void onGetNewsKindSuccess( List<NewsKindBean> newsKindBeanList) {

        initTabAndFragmentWithNewsKind(newsKindBeanList);

    }

    @Override
    public void onGetNewsKindError(String tip) {
        ToastUtil.showShort(getActivity(),tip);
    }

    @Override
    public void setPresenter(NewsHomeContract.Presenter presenter) {

    }

    @Override
    public void showLoading() {
        showProgressDialog(false,null);
    }

    @Override
    public void dismissLoading() {
        dismissProgressDialog();
    }
}

